// pages/common/industry-search/index.js
import {getIndustryList} from "../../../service/Classification"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value: "",
        isDisabled: false,
        parent: {
            show: false,
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: [],
            current: -1
        },
        child: {
            show: false,
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: [],
            current: -1
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._search = {pNo: "one"};
        this.setData({
            "parent.show": true,
            "parent.loading": true
        })
    },

    /*
    * 简体列表加载
    * */
    onLoadList: async function (e) {
        let name = e.currentTarget.dataset.name;
        this.data._name = name;
        try {
            let search = this.data._search || {};
            let paging = e.detail.paging;
            let list = this.data[name].list;
            let isReset = this.data[name].reset;
            let postData = Object.assign({
                page: paging.page,
                limit: paging.pageSize
            }, search);
            let res = await getIndustryList(postData);
            let newList = res.data;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                [name + ".list"]: list,
                [name + ".total"]: res.count
            });
        } catch (e) {
            this.setData({
                [name + ".error"]: true
            })
        } finally {
            this.setData({
                [name + ".reset"]: false,
                [name + ".loading"]: false
            })
        }
    },


    /*
    * 搜索
    * */
    onSearch: function (e) {
        let industryName = e.detail;
        industryName ? this.data._search = {industryName} : null;
        this.setData({
            "parent.show": false,
            "child.show": true,
            "child.reset": true
        })
    },
    onCancel: function () {
        this.data._search = {pNo: "one"};
        this.setData({
            "child.show": false,
            "parent.show": true,
            "parent.reset": true
        })
    },

    /*
    * 父类选择
    * */
    bindSelected: function (e) {
        const that = this;
        let name = e.currentTarget.dataset.name;
        let index = e.currentTarget.dataset.index;
        let current = this.data[name].current;
        let list = this.data[name].list;
        let currentData = list[index];
        if (name === "parent") {
            this.data._search = currentData.industryNo ? {pNo: currentData.industryNo} : null;
            this.setData({
                "parent.show": false,
                "parent.current": index,
                "child.show": true,
                "child.reset": true
            })
        } else {
            if (index === current) {return false};
            this.setData({[name + ".current"]: index});
            clearTimeout(this.TIME_OUT);
            this.TIME_OUT = setTimeout(function () {
                const eventChannel = that.getOpenerEventChannel();
                eventChannel.emit("acceptDataFromOpenedPage", currentData);
                wx.navigateBack();
            }, 500)
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let name = this.data._name;
        this.setData({[name + ".loading"]: true})
    }
})